package com.net.protocol.message;

import com.net.protocol.exception.IllegalMessageException;

import java.io.UnsupportedEncodingException;

/**
 * 消息的构建
 */
public class MessageBuilder {

    /**
     * 请求消息
     *
     * @param askBody 请求消息体
     */
    public static Message build(Message.Body.Sender.ASK askBody) throws IllegalMessageException {
        Message msg = new Message();
        try {
            msg.setHeader(new Message.Header(String.valueOf(0), askBody.getSize()));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new IllegalMessageException("消息编码格式不正确");
        }
        msg.setBody(askBody);
        return msg;
    }

    /**
     * 应答消息
     *
     * @param ackBody 应答消息体
     */
    public static Message build(Message.Body.Sender.ACK ackBody) throws IllegalMessageException {
        Message msg = new Message();
        try {
            msg.setHeader(new Message.Header(String.valueOf(0), ackBody.getSize()));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new IllegalMessageException("消息编码格式不正确");
        }
        msg.setBody(ackBody);
        return msg;
    }

}
